//点击首页培训
$(function() {
	//弹窗
	var chenggong = $(".yyc");
	//孕周
	var yunzhou = "";
	//课程id
	var xianxiapeixunid = "";
	//课程价格
	var jiage = "";
	//余额
	var yue = "";
	//弹窗
	var tanchuang = $("#xianxiapeixun>article>.yycg");
	//选择孕周
	var wanshan = $("#xianxiapeixun>article>.yycg>p:nth-of-type(1)");
	//前往完善个人资料
	var yunzhou_ziliao = $("#xianxiapeixun>article>.yycg>p:nth-of-type(2)");
	//完善资料前往按钮
	var wanshan_qianwang = $("#wanshan_button");
	//支付
	var zhifu = $("#xianxiapeixun>article>.yycg>p:nth-of-type(3)");
	//选择孕周
	var select = $("#yunzhou").next();
	//余额不足
	var buzu = $("#xianxiapeixun>article>.yycg>p:nth-of-type(4)");
	//点击前往
	var qianwang = $("#xianxiapeixun>article>.yycg>p:nth-of-type(4)>a");
	//余额不足取消
	var buzu_quxiao = buzu.find("b");

	//	***********支付***********  //
	//选择孕周后确认是否支付
	select.click(function() {
		yunzhou_ziliao.hide();
		zhifu.show();
	})
	//确认支付
	var queren = $("#xianxiapeixun>article>.yycg>p:nth-of-type(3)>b:first-of-type");
	//取消支付
	var quxiao = $("#xianxiapeixun>article>.yycg>p:nth-of-type(3)>b:last-of-type");
	//点击确认
	queren.click(function() {
		//影藏弹窗
		zhifu.hide();
		tanchuang.hide();
	})
	//点击确认支付
	queren.click(function() {
		new Ajax("/account/yue").keyvalue("myid", RssUser.Data.myid).getJson(function(data) {
			yue = data[0].availablefunds;

			yunzhou = $("#yunzhou").val();
			jiage = jiage * 1;
			yue = yue * 1;
			//检测余额是否够
			if(jiage <= yue) {
				//判断是否已经购买过课程
				//已购买
				new Ajax("/account/baoming").keyvalue("myid", RssUser.Data.myid).keyvalue("xianxiapeixunid", xianxiapeixunid).keyvalue("weeks", yunzhou).get(function(data) {
					if(data == "1") {
						alert("您已近报名预约过该课程");
						zhifu.hide();
						tanchuang.hide();
					} else {
						new Ajax("/order/buy").keyvalue("myid", RssUser.Data.myid).keyvalue("kechengid", xianxiapeixunid).keyvalue("target", "peixun").get(function(data) {
							if(data == "6") {
								//预约成功后改变报名颜色
								var baoming = $("#xianxiapeixun>article>ul>li>i");
								chenggong.show();
								baoming.text("购买成功");
								baoming.css({
									color: "white",
									background: "#ccc"
								});
								shuaxing();
							}
						})
					}
				})
				//未购买
			} else {
				tanchuang.show();
				zhifu.hide();
				buzu.show();

			}
		});
	})
	//点击取消
	quxiao.click(function() {
		//影藏弹窗
		zhifu.hide();
		tanchuang.hide();
	})
	//点击前往
	qianwang.click(function() {
		buzu.hide();
		tanchuang.hide();
		$("#win").find("a").attr("href", "#xianxiapeixun");
	})
	//点击完善前往
	wanshan_qianwang.click(function() {
		$("#xiugai_button").attr("href", "#xianxiapeixun");
	})
	//余额不足
	buzu_quxiao.click(function() {
		//取消
		buzu.hide();
		tanchuang.hide();
	})
	//	*********支付结束***********  //

	var id = "";
	var img = $("#xianxiapeixun>article>div>img");
	var section = $("#xianxiapeixun>article>ul");
	var txt = "";
	$("#xianxiapeixun>article>.yycg>p>a").click(function() {
		tanchuang.hide();
		$("#gerenxinxi>article>.yycg>p>a").attr("href", "#xianxiapeixun");
	})

	// *************点击首页的线下培训课程进入 ********************//	
	$(".gy>.liebiao>ul,#peixunliebiao>ul,#kechengpeixun>article>.xixi>.xianxia>.yuyue>ul").on("click", "li", function() {
		section.empty();
		id = $(this).attr("dataid");
		new Ajax("/xianxia").keyvalue("myid", RssUser.Data.myid).keyvalue("id", id).getJson(function(json) {
			$.each(json, function(key, val) {
				var zhuyi = val["shixiang"];
				if(zhuyi == undefined) {
					zhuyi = "";
				}
				var start = new Date(val["starttime"] * 1000).toLocaleString().substr(0, 10);;
				var endtime = new Date(val["endtime"] * 1000).toLocaleString().substr(0, 10);;
				img.attr("src", "http://file.ixinyun.cn/" + val["ico"]);
				txt = "<li><span>" + start + "</span>-<span>" + endtime + "</span><b>" + val["title"] + "</b><i jiage='" + val["jiage"] + "' xianxiapeixunid='" + val["id"] + "'>报名预约</i><span>￥" + val["jiage"] + "</span></li><li><b>注意事项：</b><div>" + zhuyi + "</div></li>"
			});
			section.html(txt);
			section.find("i").click(function() {
				//获取课程id
				xianxiapeixunid = $(this).attr("xianxiapeixunid");
				//获取课程价格
				jiage = $(this).attr("jiage");
				new Ajax("/detection").keyvalue("myid", RssUser.Data.myid).get(function(data) {
					if(data == "1") {
						wanshan.hide();
						yunzhou_ziliao.show();
						tanchuang.show();

					} else {
						wanshan.show();
						yunzhou_ziliao.hide();
						tanchuang.show();
					}
				})
			})
		})
	})

	// *************** 结束  ********************//

	// ************** 填写孕周******************//
	var txt = "";
	for(var i = 1; i <= 40; i++) {
		txt += "<option value='" + i + "'>" + i + "周</option>"
	}
	$("#yunzhou").append(txt);
})
// **************结束*********************//